import matplotlib.pyplot as plt
import matplotlib
from matplotlib.ticker import FuncFormatter
import numpy as np

# 设置中文字体和负号正常显示
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False

label_list = ['[0,0.5]', '[0.5,1]', '[1,1.5]', '[1.5,2]','[2,2.5]','[2.5,3]','[3,3.5]','[3.5,4]']  # 横坐标刻度显示值
num_list1 = [4415, 46478, 117260, 42752, 50564, 252177, 358560, 275945] # 纵坐标值1
#num_list2 = [0, 69703, 100649, 61284, 52664, 73586, 553563, 236702]  # 纵坐标值2
num_list2 = [0, 69703, 100649, 61284, 52664, 223586, 403563, 236702]  # 纵坐标值2
x = range(len(num_list1))
"""
绘制条形图
left:长条形中点横坐标
height:长条形高度
width:长条形宽度，默认值0.8
label:为后面设置legend准备
"""
color_a = np.array([254, 129, 125, 0]) / 255
color_b = np.array([129, 184, 223, 0]) / 255
rects1 = plt.bar(left=x, height=num_list1, width=0.35, alpha=1.0, color=color_a, label="gt_width")
rects2 = plt.bar(left=[i + 0.35 for i in x], height=num_list2, width=0.35, alpha=1.0, color=color_b, label="pred_width")
#plt.ylim(0, 50)  # y轴取值范围
font2 = {'family' : 'Times New Roman',
'weight' : 575,
'size'   : 28,
}

plt.tick_params(labelsize=24,length=4)
#matplotlib.rcParams['axes.labelsize'] = 8
plt.ylabel("Number",font2)
plt.xlabel("Grasp Width",font2)
"""
设置x轴刻度显示值
参数一：中点坐标
参数二：显示值
"""
plt.xticks([index + 0.2 for index in x], label_list)
# plt.xlabel("")
# plt.title("")
plt.rc('font', size=15)


font1 = {'family' : 'Times New Roman',
'weight' : 575,
'size'   : 25,
         }
plt.legend(handles=[rects1,rects2],prop=font1)  # 设置题注
# 编辑文本
# for rect in rects1:
#     height = rect.get_height()
#     plt.text(rect.get_x() + rect.get_width() / 2, height + 1, str(height), ha="center", va="bottom")
# for rect in rects2:
#     height = rect.get_height()
#     plt.text(rect.get_x() + rect.get_width() / 2, height + 1, str(height), ha="center", va="bottom")
ax = plt.gca()
ax.yaxis.get_major_formatter().set_powerlimits((0,1))

#def formatnum(x, pos):
#    return '$%.1f$x$10^{5}$' % (x/100000)
#formatter = FuncFormatter(formatnum)
#ax.yaxis.set_major_formatter(formatter)

plt.show()
